#define  _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
void QueueInit(QNode* pq)
{
	assert(pq);

	pq->head = NULL;
	pq->tail = NULL;
}

void QueueDestory(QNode* pq)
{
	assert(pq);
	Queue* curr = pq->head;
	while (curr)
	{
		Queue* del = curr;
		curr = curr->next;
		free(del);
	}
	pq->head = NULL;
	pq->tail = NULL;
}

Queue* BuyQueueNode(QDataType x)
{
	Queue* pn = (Queue*)malloc(sizeof(Queue));
	if (pn)
	{
		pn->data = x;
		pn->next = NULL;
		return pn;
	}
	else
	{
		perror("malloc");
		exit(-1);
	}
}

void QueuePush(QNode* pq, QDataType x)
{
	assert(pq);

	Queue* newnode = BuyQueueNode(x);
	if (pq->head == NULL)
	{
		pq->tail=pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}

void QueuePop(QNode* pq)
{
	assert(pq);
	assert(pq->head);

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		Queue* del = pq->head;
		pq->head = pq->head->next;
		free(del);
	}
}

QDataType QueueFront(QNode* pq)
{
	assert(pq);
	assert(pq->head);

	return pq->head->data;
}

QDataType QueueBack(QNode* pq)
{
	assert(pq);
	assert(pq->tail);

	return pq->tail->data;
}

int QueueSize(QNode* pq)
{
	assert(pq);

	Queue* curr = pq->head;
	int size = 0;
	while (curr)
	{
		size++;
		curr = curr->next;
	}
	return size;
}

bool QueueEmpty(QNode* pq)
{
	assert(pq);

	return pq->head == NULL;
}